<template>
	<view class="cart-container">
		
	 <!-- @num-change="numberChangeHandler" @raido-change="radioChangeHandler" -->
	
	    <hello></hello>
		<!-- 购物车商品列表的标题区域 -->
		<view class="cart-title">
		  <!-- 左侧的图标 -->
		  <uni-icons type="shop" size="18"></uni-icons>
		  <!-- 描述文本 -->
		  <text class="cart-title-text">购物车</text>
		</view>
		<uni-swipe-action>
			<block v-for="(goods,i) in cart" :key ="i">
				<uni-swipe-action-item :options="options" @click="swipeActionClickHandler(goods)">
					<my-goods :goods="goods" :show-radio="true" :show-num="true"    @raido-change="radioChangeHandler"></my-goods>
				</uni-swipe-action-item>
				
			</block>
		</uni-swipe-action>
		
		<my-settle></my-settle>
	</view>
</template>

<script>
	import badgeMix from '../../mixins/tabbar-badge.js'
	 
	import { mapGetters,mapState,mapMutations } from 'vuex'
	export default {
		
		mixins: [badgeMix],
		
		data() {
			return {
				options:[{
					text:'删除',
					style:{
						backgroundColor: '#C00000'
					}
				}]
				
			}
		},
		watch:{
			  total(){
				  this.setBadge()
			  },
			  
		},
		onShow() {
			this.setBadge()
		},
		computed:{
			...mapState('m_cart',['cart']),
			...mapGetters('m_cart',['total'])
		},
		methods: {
				...mapMutations('m_cart',['updateGoodsState','updateGoodsCount','removeGoodsById']),
		
			     radioChangeHandler(e){
			
				 // 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
			     
				  // 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
			
						  this.updateGoodsState(e)
					  },
				  
			   numberChangeHandler(e){
				   this.updateGoodsCount(e)
			   },
			
			setBadge(){
				uni.setTabBarBadge({
					index:2,
					text:this.total + ''
				})
			},
			swipeActionClickHandler(goods){
				this.removeGoodsById(goods.goods_id)
			},
		}
	}
</script>

<style lang="scss">
.cart-title {
  height: 40px;
  display: flex;
  align-items: center;
  font-size: 14px;
  padding-left: 5px;
  border-bottom: 1px solid #efefef;
  .cart-title-text {
    margin-left: 10px;
  }
}
.goods-item {
  // 让 goods-item 项占满整个屏幕的宽度
  width: 750rpx;
  // 设置盒模型为 border-box
  box-sizing: border-box;
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
}
.cart-container {
  padding-bottom: 50px;
}
</style>
